//
//  UIButton+Eextension.swift
//  XYReadBook
//
//  Created by fox on 2017/9/9.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import Foundation
enum ButtonEdgeInsetsStyle {
    case ButtonEdgeInsetsStyleTop // image在上，label在下
    case ButtonEdgeInsetsStyleLeft  // image在左，label在右
    case ButtonEdgeInsetsStyleBottom  // image在下，label在上
    case ButtonEdgeInsetsStyleRight // image在右，label在左
}

extension UIButton {
    func layoutButtonEdgeInsets(style:ButtonEdgeInsetsStyle,space:CGFloat) {
        var labelWidth : CGFloat = 0.0
        var labelHeight : CGFloat = 0.0
        var imageEdgeInset = UIEdgeInsets.zero
        var labelEdgeInset = UIEdgeInsets.zero
        let imageWith = self.imageView?.frame.size.width
        let imageHeight = self.imageView?.frame.size.height
       
        if UIDevice.current.systemVersion.doubleValue() >= 8.0 {
            labelWidth = (self.titleLabel?.intrinsicContentSize.width)!
            labelHeight = (self.titleLabel?.intrinsicContentSize.height)!
        }else {
            labelWidth = (self.titleLabel?.frame.size.width)!
            labelHeight = (self.titleLabel?.frame.size.height)!
        }
        
        
        switch style {
        case .ButtonEdgeInsetsStyleTop:
            imageEdgeInset = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth)
            labelEdgeInset = UIEdgeInsetsMake(0, -imageWith!, -imageHeight!-space/2.0, 0)
        case .ButtonEdgeInsetsStyleLeft:
            imageEdgeInset = UIEdgeInsetsMake(0, -space / 2.0, 0, space/2.0);
            labelEdgeInset = UIEdgeInsetsMake(0, space / 2.0, 0, -space/2.0);
        case .ButtonEdgeInsetsStyleBottom:
            imageEdgeInset = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth)
            labelEdgeInset = UIEdgeInsetsMake(-imageHeight!-space/2.0, -imageWith!, 0, 0)
        case .ButtonEdgeInsetsStyleRight:
            // To Do print("坐标是====\(labelWidth)=====\(space)")
            imageEdgeInset = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0)
            labelEdgeInset = UIEdgeInsetsMake(0, -imageWith! - space / 2.0, 0, imageWith!+space/2.0)
        }
        self.titleEdgeInsets = labelEdgeInset
        self.imageEdgeInsets = imageEdgeInset
    }
}
